//
//  RYYuSuyuanCell.swift
//  SheYangBigData
//
//  Created by 宋海胜 on 2021/1/13.
//  Copyright © 2021 叁拾叁. All rights reserved.
//



import UIKit

class RYYuSuyuanCell: SYBaseCell {

    var bgView: UIView = {
        let v: UIView = UIView()
        v.backgroundColor = .white
        return v
    }()
    
    var xuxian: UIView = {
        let v: UIView = UIView(frame: CGRect(x: 0, y: 0, width: 2, height: 42.autoWidth()))
        v.isHidden = true
        return v
    }()
    var shixian: UIView = {
        let v: UIView = UIView()
        v.isHidden = true
        return v
    }()
    
    let firLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0x367FF6)
        v.font = 13.autoFontSize()
        
        return v
    }()
    
    let secLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0x666666)
        v.font = 12.autoFontSize()
        
        return v
    }()
    
    let pic: UIImageView = {
        let v: UIImageView = UIImageView()
        
        return v
    }()

    
    func configure(_ model: TraceRecords, _ isYu: Bool) {
        firLb.text = model.name
        secLb.text = model.createDate
        
        if isYu {
            if model.name == "孵化" {
                if model.status == 1 {
                    pic.image = UIImage(named: "fuhua_select")
                } else {
                    pic.image = UIImage(named: "fuhua")
                }
                xuxian.isHidden = true
                shixian.isHidden = true
                bgView.snp.remakeConstraints { (make) in
                    make.top.bottom.equalToSuperview()
                    make.left.equalToSuperview()
                    make.right.equalToSuperview()
                    make.height.equalTo(25.autoWidth())
                }
            } else if model.name == "破卵" {
                if model.status == 1 {
                    pic.image = UIImage(named: "poruan_select")
                    xuxian.isHidden = false
                    shixian.isHidden = true
                } else {
                    pic.image = UIImage(named: "poruan")
                    xuxian.isHidden = true
                    shixian.isHidden = false
                }
            } else if model.name == "育苗" {
                if model.status == 1 {
                    pic.image = UIImage(named: "yumiao_select")
                    xuxian.isHidden = false
                    shixian.isHidden = true
                } else {
                    pic.image = UIImage(named: "yumiao")
                    xuxian.isHidden = true
                    shixian.isHidden = false
                }
            } else if model.name == "发育" {
                if model.status == 1 {
                    pic.image = UIImage(named: "fayu_select")
                    xuxian.isHidden = false
                    shixian.isHidden = true
                } else {
                pic.image = UIImage(named: "fayu")
                    xuxian.isHidden = true
                    shixian.isHidden = false
                }
            } else if model.name == "捕捞" {
                if model.status == 1 {
                    pic.image = UIImage(named: "bulao_select")
                    xuxian.isHidden = false
                    shixian.isHidden = true
                } else {
                    pic.image = UIImage(named: "bulao")
                    xuxian.isHidden = true
                    shixian.isHidden = false
                }
                bgView.snp.remakeConstraints { (make) in
                    make.top.bottom.equalToSuperview()
                    make.left.equalToSuperview()
                    make.right.equalToSuperview()
                    make.height.equalTo(85.autoWidth())
                }
                pic.snp.remakeConstraints { (make) in
                    make.bottom.equalToSuperview().offset(-20.01.autoWidth())
                    make.left.equalToSuperview().offset(53.autoWidth())
                    make.width.height.equalTo(23.autoWidth())
                }
            }
        } else {
            if model.name == "播种" {
                if model.status == 1 {
                    pic.image = UIImage(named: "bozhong_select")
                } else {
                    pic.image = UIImage(named: "bozhong")
                }
                xuxian.isHidden = true
                shixian.isHidden = true
                bgView.snp.remakeConstraints { (make) in
                    make.top.bottom.equalToSuperview()
                    make.left.equalToSuperview()
                    make.right.equalToSuperview()
                    make.height.equalTo(25.autoWidth())
                }
                
            } else if model.name == "抽芽" {
                if model.status == 1 {
                    pic.image = UIImage(named: "chouya_select")
                    xuxian.isHidden = false
                    shixian.isHidden = true
                } else {
                    pic.image = UIImage(named: "chouya")
                    xuxian.isHidden = true
                    shixian.isHidden = false
                }
            } else if model.name == "育苗" {
                if model.status == 1 {
                    pic.image = UIImage(named: "yumiao_cai_select")
                    xuxian.isHidden = false
                    shixian.isHidden = true
                } else {
                    pic.image = UIImage(named: "yumiao_cai")
                    xuxian.isHidden = true
                    shixian.isHidden = false
                }
            } else if model.name == "茎叶成长期" {
                if model.status == 1 {
                    pic.image = UIImage(named: "jingye_select")
                    xuxian.isHidden = false
                    shixian.isHidden = true
                } else {
                    pic.image = UIImage(named: "jingye")
                    xuxian.isHidden = true
                    shixian.isHidden = false
                }
            } else if model.name == "采收" {
                if model.status == 1 {
                    pic.image = UIImage(named: "caishou_select")
                    xuxian.isHidden = false
                    shixian.isHidden = true
                } else {
                    pic.image = UIImage(named: "caishou")
                    xuxian.isHidden = true
                    shixian.isHidden = false
                }
                bgView.snp.remakeConstraints { (make) in
                    make.top.bottom.equalToSuperview()
                    make.left.equalToSuperview()
                    make.right.equalToSuperview()
                    make.height.equalTo(85.autoWidth())
                }
                pic.snp.remakeConstraints { (make) in
                    make.bottom.equalToSuperview().offset(-20.01.autoWidth())
                    make.left.equalToSuperview().offset(53.autoWidth())
                    make.width.height.equalTo(23.autoWidth())
                }
            }
        }
        
    }
    
    override func initViews() {
        
        addSubview(bgView)
        bgView.addSubview(firLb)
        bgView.addSubview(secLb)
        bgView.addSubview(pic)
        bgView.addSubview(xuxian)
        bgView.addSubview(shixian)
        self.drawDashLine(lineView: xuxian, strokeColor: UIColor(hex: 0x367FF6), lineWidth: 2, lineLength: 2, lineSpacing: 4)
        bgView.snp.makeConstraints { (make) in
            make.top.bottom.equalToSuperview()
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(65.autoWidth())
        }
        
        firLb.snp.makeConstraints { (make) in
            make.centerY.equalTo(pic)
            make.left.equalTo(pic.snp.right).offset(10.autoWidth())
            make.height.equalTo(15.autoWidth())
        }
        secLb.snp.makeConstraints { (make) in
            make.centerY.equalTo(pic)
            make.left.equalTo(pic.snp.right).offset(115.autoWidth())
            make.height.equalTo(15.autoWidth())
        }
        pic.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(0.01.autoWidth())
            make.left.equalToSuperview().offset(53.autoWidth())
            make.width.height.equalTo(23.autoWidth())
        }
        xuxian.snp.makeConstraints { (make) in
            make.bottom.equalTo(pic.snp.top)
            make.centerX.equalTo(pic)
            make.top.equalToSuperview()
            make.width.equalTo(2.autoWidth())
        }
        shixian.snp.makeConstraints { (make) in
            make.bottom.equalTo(pic.snp.top)
            make.centerX.equalTo(pic)
            make.top.equalToSuperview()
            make.width.equalTo(2.autoWidth())
        }
    }

    /// 绘制虚线
        /// - Parameters:
        ///   - lineView: 添加虚线的view
        ///   - strokeColor: 虚线颜色
        ///   - lineWidth: 虚线宽度
        ///   - lineLength: 每段虚线的长度
        ///   - lineSpacing: 每段虚线的间隔
        private func drawDashLine(lineView:UIView,
                                  strokeColor: UIColor,
                                  lineWidth: CGFloat = 0.5,
                                  lineLength: Int = 4,
                                  lineSpacing: Int = 4) {
            let shapeLayer = CAShapeLayer()
            shapeLayer.bounds = lineView.bounds
            shapeLayer.position = CGPoint(x: lineView.frame.size.width/2, y: lineView.frame.size.height/2)
            shapeLayer.fillColor = UIColor.blue.cgColor
            shapeLayer.strokeColor = strokeColor.cgColor
            
            shapeLayer.lineWidth = lineWidth
            shapeLayer.lineJoin = CAShapeLayerLineJoin.round
            shapeLayer.lineDashPhase = 0
            //每一段虚线长度 和 每两段虚线之间的间隔
            shapeLayer.lineDashPattern = [NSNumber(value: lineLength), NSNumber(value: lineSpacing)]
            
            let path = CGMutablePath()
            //起点
            path.move(to: CGPoint(x: lineView.frame.size.width/2, y: 0))

            //终点
            ///  横向 y = 0
    //        path.addLine(to: CGPoint(x: 0, y: 0))
            ///纵向 Y = view 的height
            path.addLine(to: CGPoint(x: lineView.frame.width/2, y: lineView.frame.height))
            shapeLayer.path = path
            lineView.layer.addSublayer(shapeLayer)
        }
}
